<template>
  <div>
    <van-button
      :icon="value === 1 ? 'good-job' : 'good-job-o'"
      :class="{
        liked: value === 1
      }"
      :loading="loading"
      @click="oncLickArticle"
    />
  </div>
</template>

<script>
import { addLikeArticle, deleteLikeArticle } from '@/api/article'
export default {
  name: 'LikeArticle',
  components: {},
  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data() {
    return {
      loading: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async oncLickArticle() {
      this.loading = true
      try {
        let status = -1
        if (this.value === 1) {
          // 已点赞 取消点赞
          await deleteLikeArticle(this.articleId)
        } else {
          // 未点赞 点击点赞
          await addLikeArticle(this.articleId)
          status = 1
        }
        this.$emit('input', status)

        this.$toast(status === 1 ? '点赞成功' : '取消点赞')
      } catch (error) {
        this.$toast.fail('操作失败，请重试！')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped lang="less">
.liked {
  .van-icon {
    color: #e5645f;
  }
}
</style>
